package com.jfire.socket.socketserver.interceptor.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.jfire.socket.socketserver.bus.BusinessCommand;
import com.jfire.socket.socketserver.bus.Message;
import com.jfire.socket.socketserver.interceptor.MessageInterceptor;

public class AuthInterceptor implements MessageInterceptor
{
    private Logger logger = LogManager.getLogger();
    
    @Override
    public int getOrder()
    {
        return Integer.MIN_VALUE;
    }
    
    @Override
    public int inInterceptor(Message message)
    {
        if (message.getChannelInfo().isAuthentication())
        {
            return MessageInterceptor.next;
        }
        else
        {
            if (message.getCommand() == BusinessCommand.AUTH || message.getCommand() == BusinessCommand.SENDKEY)
            {
                return MessageInterceptor.end;
            }
            else
            {
                logger.warn("未授权异常线路，地址为{}。关闭该线路。", message.getChannelInfo().getAddress());
                message.getChannelInfo().close();
                return MessageInterceptor.interceptored;
            }
        }
    }
    
    @Override
    public int outInterceptor(Message message)
    {
        return MessageInterceptor.end;
    }
    
}
